package com.uenpay.im.ui.old.activity.chat;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.base.BaseActivity;
import com.uenpay.im.adapter.LocalMusicAdapter;
import com.uenpay.im.entities.SendFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

/**
 * @author SL
 * @date 2014/7/23
 * @copyRight http://www.qiyunxin.com 本地音乐
 */
public class LocalMusicActivity extends BaseActivity {
	private TextView title;
	private ArrayList<SendFile> mlList = new ArrayList<SendFile>();
	private TextView send_file_btn;
	private SendFile mSendFile = null;
	private ListView mListView;
	private LinearLayout no_music_layout;
	private LocalMusicAdapter mAdapter;
	private Calendar mCalendar = Calendar.getInstance();
	private SimpleDateFormat formatter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_local_music_list);
		IMApplication.getInstance().addActivity(this);
		formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.getDefault());
		no_music_layout = (LinearLayout) findViewById(R.id.no_music_layout);

		mListView = (ListView) findViewById(R.id.local_file_list);
		title = (TextView) findViewById(R.id.title_textview);
		backListener();
		send_file_btn = (TextView) findViewById(R.id.title_right_tv);
		findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
		send_file_btn.setText(getResources().getString(R.string.send_file));
		findViewById(R.id.title_right_layout).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						if (mSendFile == null) {
							return;
						}
						Intent data = new Intent();
						data.putExtra("send_file", mSendFile);
						setResult(RESULT_OK, data);

						LocalMusicActivity.this.finish();
					}
				});
		boolean is_music = getIntent().getBooleanExtra("is_music", true);
		if (is_music) {
			title.setText(getResources().getString(R.string.choose_music_file));
			queryAllMusic();
		} else {
			title.setText(getResources().getString(R.string.choose_image_file));
		}

		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mSendFile = (SendFile) mListView.getItemAtPosition(position);
				if (mAdapter != null) {
					mAdapter.setSelectedItem(position);
				}
			}
		});

		//返回键点击
		findViewById(R.id.back_layout).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

	}

	private void queryAllMusic() {
		Cursor cursor = getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				new String[] { MediaStore.Audio.Media.TITLE,
						MediaStore.Audio.Media.DATA,
						MediaStore.Audio.Media.SIZE,
						MediaStore.Audio.Media.DISPLAY_NAME,
						MediaStore.Audio.Media.DATE_ADDED, }, null, null, null);
		if (cursor != null && cursor.moveToFirst()) {
			do {
				String mName = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.TITLE));
				String mData = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.DATA));
				String mSize = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.SIZE));
				String date = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED));
				String disply_name = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));

				int dotIndex = disply_name.lastIndexOf(".");
				if (dotIndex < 0) {
					continue;
				}
				String end = "";
				try {
					end = disply_name.substring(dotIndex, disply_name.length())
							.toLowerCase(Locale.getDefault());
					if (TextUtils.isEmpty(mSize)) {
						continue;
					}
				} catch (Exception e) {
					end = ".qiyunxin";
				}

				mCalendar.setTimeInMillis(Long.valueOf(date) * 1000);
				String add_date = formatter.format(mCalendar.getTime());

				SendFile mSendFile = new SendFile();
				mSendFile.filePath = mData;
				mSendFile.fileSize = mSendFile.getFileSize(mSize);
				mSendFile.fileName = mName + end;
				mSendFile.fileAddDate = add_date;
				mlList.add(mSendFile);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}

		if (mlList != null) {
			if (mlList.size() < 1) {
				no_music_layout.setVisibility(View.VISIBLE);
				send_file_btn.setVisibility(View.GONE);
				return;
			}

			mAdapter = new LocalMusicAdapter(this, mlList);
			mListView.setAdapter(mAdapter);
		}
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}

	@Override
	protected void initView() {
	}

	@Override
	protected void initListener() {
	}

}
